<template>
  <div class="pd30 orglist flex flexjcsb">
    <div class="panelWrap">
      <div class="title flex flexac flexjcsb fs14 cfff fwb pl p-r-20">
        <div>机构管理平台</div>
      </div>
      <div class="con">
        <div class="con-row flex flexac pl search-box">
          <!-- <div class="inputLable inputLable2 flex flexac">
            公司：
            <el-input v-model="companyName" readonly placeholder="请选择所属公司..." />
            <el-button type="warning" class="comp-select m-l-20 iconfont icon-xuanze" @click="handleSelect">选择公司</el-button>
          </div> -->
          <div class="flex flexac">
            <el-input v-if="activeName === '0'" v-model="param.name" class="org-input" placeholder="请输入机构名称..." @input="getOrgList" />
            <el-input v-if="activeName === '1'" v-model="param.name" class="org-input" placeholder="请输入数据角色名称..." @input="getDataRoles" />
          </div>
          <div class="groupbtn flex m-l-40">
            <el-button v-if="activeName === '0'" class="flex flexac m-r-20 fs14" size="small" type="warning" @click="getOrgList"><i class="co-i" /><span class="search1">查 询</span></el-button>
            <el-button v-if="activeName === '1'" class="flex flexac m-r-20 fs14" size="small" type="warning" @click="getDataRoles"><i class="co-i" /><span class="search1">查 询</span></el-button>
            <el-button class="flexcenter co-empty m-l-25" type="info" @click="handleEmptySearch"><i class="iconfont icon-qingkong" />清 空</el-button>
          </div>
          <div class="groupbtn flex m-l-60 ">
            <!-- <div class="selectBtn flex flexac " @click="getOrgList">查询</div> -->
            <!-- <el-button class="emptyBtn flexcenter m-l-25" type="info" @click="handleEmptySearch"><i class="iconfont icon-qingkong" />清空</el-button> -->
          </div>
        </div>
        <div class="con-row flex flexac flexjcsb pl">
          <el-button class="defaultbtn" type="primary" @click="handleRefresh"><i class="el-icon-refresh" :class="refresh?'goRefresh':''" />刷 新</el-button>
          <div class="m-r-25 flex">
            <el-button class="defaultbtn" type="primary" @click="handleAdd"><i class="el-icon-circle-plus-outline" />添加机构</el-button>
            <el-button class="defaultbtn" type="primary" @click="handleExportExcel"><i class="iconfont icon-wenjiandaochu" />导出</el-button>
            <div class="pre m-l-10">
              <el-button class="defaultbtn pre" type="primary" @click="handleSenior('field')"><i class="iconfont icon-shezhi" />字段
              </el-button>
              <div class="selectUl" :class="seniorSet.field?'showSelectUl':'hideSelectUl'">

                <div v-for="(item,i) in tablehead" :key="i" class="li">
                  <el-checkbox v-model="item.isShow">{{ item.label }}</el-checkbox>
                </div>
              </div>
            </div>
            <div class="pre m-l-10">
              <el-button class="defaultbtn" type="primary" @click="handleSenior('sortable')"><i class="iconfont icon-shezhi" />排序</el-button>
              <div class="selectUl" :class="seniorSet.sortable ? 'showSelectUl' : 'hideSelectUl'">

                <div v-for="(item,i) in tablehead" v-if="item.isShow" :key="i" class="li">
                  <el-checkbox v-model="item.sortable">{{ item.label }}</el-checkbox>
                </div>
              </div>
            </div>
            <!-- <el-button class="defaultbtn" @click="handleExportExcel" type="primary"><i class="iconfont icon-shezhi"></i>固定</el-button> -->
          </div>
        </div>

        <el-tabs v-model="activeName" class="tabs" type="border-card" @tab-click="handleClick">
          <el-tab-pane v-for="items in options" :key="items.id" :label="items.value" :name="items.id">
            <div v-if="items.id === '0'">
              <el-table
                id="orgList"
                ref="orgList"
                class="con-table"
                type="expand"
                :data="orgList"
                highlight-current-row
              >

                <template v-for="(item, i) in tablehead">
                  <el-table-column v-if="item.isShow" :key="i" :width="item.width" align="center" :sortable="item.sortable" :show-overflow-tooltip="true" :prop="item.prop" :label="item.label" />
                </template>

                <el-table-column prop="name" align="center" width="200" label="基本操作">
                  <template slot-scope="scope">
                    <el-button class="defaultbtn m-r-10" type="primary" @click="handleEdit(scope.$index, scope.row)">
                      <i class="el-icon-edit" />编辑</el-button>
                    <el-button slot="reference" class="defaultbtn" type="danger" @click="handleDeleteBtn(scope.$index, scope.row)">
                      <i class="el-icon-delete" />删除</el-button>
                  </template>
                </el-table-column>
              </el-table>
              <el-pagination
                class="paginationWrap flex"
                :current-page="paging.pageIndex"
                :page-sizes="paging.pageSizeArr"
                :page-size="paging.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="paging.total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </div>
            <!-- 数据角色 -->
            <div v-else>
              <el-table
                id="dataRoles"
                ref="dataRoles"
                class="con-table"
                type="expand"
                :data="dataRoles"
              >

                <template v-for="item in tablerolehead">
                  <el-table-column v-if="item.isShow" :key="item.index" :width="item.width" align="center" :sortable="item.sortable" :show-overflow-tooltip="true" :prop="item.prop" :label="item.label" />
                </template>
              </el-table>

              <el-pagination
                class="paginationWrap flex"
                :current-page="pagings.pageIndex"
                :page-sizes="[10]"
                :page-size="pagings.pageSize"
                :pager-count="5"
                layout="total, sizes, prev, pager, next, jumper"
                :total="pagings.total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </div>
          </el-tab-pane>
        </el-tabs>
      </div>
    </div>
    <OrgDiaolg v-model="visible" :data-org="editOrg" :is-edit="isEdit" @handleRefresh="handleRefresh" @handleCloseDialog="handleCloseDialog" />
  </div>
</template>

<script>

import OrgDiaolg from './dialog/index'
import { findOrgList, deleteOrg, findOrgDetail } from '@/api/baseInfo/authority/org'
import { showNotice, formatDateTime, transData } from '@/utils/toolclass'
import { findRoleList } from '@/api/baseInfo/authority/role'
// import storage from '@/utils/storage'

export default {
  components: { OrgDiaolg },
  data() {
    return {
      activeName: '0',
      options: [
        {
          id: '0',
          value: '机构列表'
        },
        {
          id: '1',
          value: '数据角色'
        }
      ],
      orgList: [],
      dataRoles: [],
      orgAllList: [],
      visible: false,
      isEdit: false,
      refresh: false,
      coSelectVisible: false,
      type: 1,
      treeList: [],
      coType: null,
      orgName: '',
      orgCategory: '',
      editOrg: {},
      companyName: '',
      queryParam: {
        roleName: '',
        companyId: ''
      },
      param: {
        id: '',
        name: ''
      },
      paging: {
        pageIndex: 1,
        pageSize: 10,
        pageSizeArr: [10, 15, 20, 35],
        total: 0
      },
      pagings: {
        pageIndex: 1,
        pageSize: 10,
        pageSizeArr: [10],
        total: 0
      },
      tablehead: [
        {
          index: 0,
          prop: 'index',
          label: '序号',
          width: '80',
          sortable: false,
          fixed: true,
          isShow: true
        },
        {
          index: 1,
          prop: 'parentId',
          label: '父级ID',
          width: '180',
          sortable: false,
          fixed: true,
          isShow: true
        },
        {
          index: 2,
          prop: 'id',
          label: '机构ID',
          width: '180',
          sortable: false,
          fixed: true,
          isShow: true
        },
        {
          index: 3,
          prop: 'name',
          label: '机构名称',
          sortable: false,
          fixed: true,
          isShow: true
        },
        {
          index: 4,
          prop: 'nameSimple',
          label: '机构简称',
          sortable: false,
          fixed: true,
          isShow: true
        }
      ],
      tablerolehead: [
        {
          index: 0,
          prop: 'index',
          label: '序号',
          width: '120',
          sortable: false,
          fixed: true,
          isShow: true
        },
        {
          index: 1,
          prop: 'id',
          label: '角色ID',
          sortable: false,
          fixed: true,
          isShow: true
        },
        {
          index: 2,
          prop: 'name',
          label: '角色名称',
          sortable: false,
          fixed: true,
          isShow: true
        }
      ],
      seniorSet: {
        'field': false,
        'sortable': false
      }
    }
  },
  computed: {},
  mounted() {
    this.$storage.set('upUrl', JSON.stringify({ 'path': '/authority/org/index' }))
    this.getOrgList()
    this.getAllOrgList()
  },
  methods: {
    handleClick() {
      if (this.activeName === '0') {
        this.getOrgList()
      } else {
        this.getDataRoles()
      }
    },
    getAllOrgList() {
      findOrgList('', '', this.paging.pageIndex, 1000000).then(res => {
        // console.log(res.data)
        if (res.code === '000000') {
          this.orgAllList = res.data
        }
      })
    },
    // 数据角色
    getDataRoles() {
      findRoleList(this.param.id, 1, this.param.name, this.pagings.pageIndex, this.pagings.pageSize).then((res) => {
        if (res.code === '000000') {
          // console.log(res)
          this.dataRoles = res.data.map((item, i) => {
            const items = {
              index: i + 1,
              id: item.id,
              name: item.name,
              version: item.version
            }
            return items
          })
          this.pagings = {
            pageIndex: res.pager.pageIndex,
            pageSize: res.pager.pageSize,
            total: res.pager.totalSize
          }
        }
      })
    },
    // 查询机构列表
    getOrgList() {
      findOrgList(this.param.id, this.param.name, this.paging.pageIndex, this.paging.pageSize).then(res => {
        if (res.code === '000000') {
          // console.log(res)
          this.orgList = res.data.map((item, i) => {
            const items = {
              index: i + 1,
              id: item.id,
              name: item.name,
              nameSimple: item.nameSimple,
              parentId: item.parentId,
              version: item.version
            }
            return items
          })
          this.paging = {
            pageIndex: res.pager.pageIndex,
            pageSize: res.pager.pageSize,
            total: res.pager.totalSize
          }
        }
      })
    },
    handleDeleteBtn(index, row) {
      this.$alert('这个操作会删除和它相关联的所有数据,确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        showCancelButton: true,
        callback: action => {
          if (action === 'confirm') {
            this.handleDelete(row.id)
          }
        }
      })
    },
    handleDelete(id) {
      // console.log(id)
      if (id <= 10) {
        showNotice('warning', '提示', '不允许删除默认机构')
        return
      }
      deleteOrg(id).then(res => {
        if (res.code === '000000') {
          showNotice('success', '提示', '删除成功')
          this.getOrgList()
        }
      })
    },
    handleEdit(index, row) {
      // console.log(row)
      this.visible = true
      this.isEdit = true
      findOrgDetail(row.id).then(res => {
        if (res.code === '000000') {
          // console.log(res)
          const parentName = this.orgAllList.filter(item => {
            // console.log(item)
            if (item.id === row.parentId) {
              return item
            }
          })
          // console.log(parentName)
          this.editOrg = {
            id: res.data.id,
            name: res.data.name,
            nameSimple: res.data.nameSimple,
            parentId:	res.data.parentId,
            parentName: parentName.length === 0 ? '' : parentName[0].name,
            version: res.data.version
          }
        }
      })
    },
    handleAdd() {
      this.visible = true
      this.isEdit = false
      this.editOrg = {
        id: 0, // ID
        name: '', // 机构名称
        nameSimple: '', // 机构简称
        parentName: '',
        parentId: '', // 父级ID
        version: null // 版本号
      }
    },
    handleExportExcel() {
      require.ensure([], () => {
        const { export_json_to_excel } = require('@/excel/Export2Excel')
        const tHeader = ['上级部门ID', '机构ID', '机构名称', '机构简称']
        const filterVal = ['parentId', 'id', 'name', 'nameSimple']
        const data = this.formatJson(filterVal, this.orgList)
        export_json_to_excel(tHeader, data, '机构列表(' + formatDateTime().ymd) + ')'
      })
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },
    handleSenior(type) {
      if (type === 'field') {
        this.seniorSet.field = !this.seniorSet.field
      }
      if (type === 'sortable') {
        this.seniorSet.sortable = !this.seniorSet.sortable
      }
    },
    handleRefresh() {
      this.refresh = true
      if (this.activeName === '0') {
        this.getOrgList()
      } else {
        this.getDataRoles()
      }
      setTimeout(() => {
        this.refresh = false
      }, 1200)
    },
    handleEmptySearch() {
      this.param.name = ''
      if (this.activeName === '0') {
        this.getOrgList()
      } else {
        this.getDataRoles()
      }
    },
    handleCloseDialog() {
      this.visible = !this.visible
    },
    handleSizeChange(val) {
      this.paging.pageSize = val
      if (this.activeName === '0') {
        this.getOrgList()
      } else {
        this.getDataRoles()
      }
    },
    handleCurrentChange(val) {
      if (this.activeName === '0') {
        this.paging.pageIndex = val
        this.getOrgList()
      } else {
        this.pagings.pageIndex = val
        this.getDataRoles()
      }
    }
  }
}
</script>

<style lang="scss">
  @import "@/styles/public.scss";

  .orglist {
    .el-tabs--border-card>.el-tabs__content {
      padding: 15px 0 0 0;
    }
    .org-input {
      width: 240px;
    }

  }

</style>
